package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.TaStock;
import sanrenxing.dayEnd.backRepertoryTotal.domain.TaStockTwo;
import sanrenxing.dayEnd.backRepertoryTotal.service.TaStockBiz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：Ta库存的控制器类
 * 作者：赵思睿
 * 日期：2021/3/17 0017-14:14
 * 版本：1.0
 */
@RestController
@RequestMapping("/taStockcrud")
public class TaStockController {
    @Autowired
    private TaStockBiz taStockBiz;

    /**
     * 分页条件查询
     * @param taStock Ta库存对象
     * @param page 页数
     * @param limit 每页行数
     * @return Ta库存集合对象
     */
    @GetMapping("/taStock")
    public Map<String, Object> selectAll(TaStock taStock, int page, int limit){
        //执行多条件分页查询
        List<TaStockTwo> taStockList=taStockBiz.selectAll(taStock,limit,(page-1)*limit);
        if (taStockList!=null && taStockList.size()!=0){
            TaStockTwo taStockTwo = taStockList.get(0);
            if (taStockBiz.counts()!=0){
                //查询昨天的基金净值是否存在
                taStockTwo.setTatMoney(taStockBiz.findYesterDayValue()!=0.0?taStockBiz.findYesterDayValue():1);
            }else taStockTwo.setTatMoney(1);

            taStockList.set(0,taStockTwo);
        }
        //多条件查询表中总记录数
        int totalRows=taStockBiz.count(taStock);

        //定义映射集合对象
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("data",taStockList);
        map.put("count",totalRows);
        //返回映射集合对象
        return map;
    }

    /**
     * 添加Ta库存
     * @param taStock Ta库存对象
     * @return 1：添加成功；0：添加失败
     */
    @PostMapping("/taStock")
    public String add(@RequestBody TaStock taStock){
        return taStockBiz.add(taStock)>0?"add success":"add failure";
    }

    /**
     * 根据Ta库存编号删除数据
     * @param tatId Ta库存编号
     * @return 1：删除成功；0：删除失败
     */
    @DeleteMapping("/taStock/{tatId}")
    public String delete(@PathVariable String tatId){
        return taStockBiz.delete(tatId)>0?"delete success":"delete failure";
    }
}
